/*
 * File:         Holiday.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package foxtrotbravo;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;

/**
 * The Class Holiday.
 *
 * @author Dude
 */
public class Holiday implements JSONString
{

  /** The email. */
  private long ID;

  /** The email. */
  private String title;

  /** The access token. */
  private String image;

  /** The expire time. */
  private Date start;

  /**
   * Instantiates a new user.
   *
   * @param id the id
   */
  public Holiday(long id)
  {
    ID = id;
    load();
  }

  /**
   * Get Title.
   *
   * @return the title
   */
  public String getTitle()
  {
    return title;
  }

  /**
   * Sets the title.
   *
   * @param Title the new title
   */
  public void setTitle(String Title)
  {
    this.title = Title;
  }

  /**
   * Get Image.
   *
   * @return the image
   */
  public String getImage()
  {
    return image;
  }

  /**
   * Sets the image.
   *
   * @param Image the new image
   */
  public void setImage(String Image)
  {
    this.image = Image;
  }

  /**
   * Get Start.
   *
   * @return the start
   */
  public Date getStart()
  {
    return start;
  }

  /**
   * Sets the start.
   *
   * @param Start the new start
   */
  public void setStart(Date Start)
  {
    this.start = Start;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.json.JSONString#toJSONString()
   */
  @Override
  public String toJSONString()
  {
    return toJSONObject().toString();
  }
  
  /**
   * To json object.
   *
   * @return the jSON object
   */
  public JSONObject toJSONObject()
  {
    JSONObject JO = new JSONObject();
    try
    {
      //Move to next year if it has passed
      GregorianCalendar yesterday = new GregorianCalendar();
      yesterday.add(Calendar.DATE, -1);
      GregorianCalendar nextYear = new GregorianCalendar();
      nextYear.setTimeInMillis(start.getTime());
      nextYear.add(Calendar.YEAR, 1);      
      if(start.getTime() < yesterday.getTimeInMillis())
        start.setTime(nextYear.getTimeInMillis());
      JO.put("id", ID);
      JO.put("title", title);
      JO.put("image", image);
      JO.put("type", "holiday");
      JO.put("start", start);
    } catch (JSONException e)
    {
      // Do Nothing
    }
    return JO;
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return toJSONString();
  }
  
  /**
   * Load.
   *
   * @return true, if successful
   */
  public boolean load() {
    try {
      if (DataConnection.connect()) {
        String statement = "select title, image, date from holiday where id = ?";
        PreparedStatement stmt = DataConnection.getConnection()
            .prepareStatement(statement);
        stmt.setLong(1, ID);
        ResultSet results = stmt.executeQuery();
        if (results.next()) {
          title = results.getString("title");
          image = results.getString("image");
          start = results.getDate("date");
          return true;
        }
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return false;
  }

  /**
   * Get All.
   *
   * @return the all
   */
  public static String getAllAsJson()
  {
    JSONArray JA = new JSONArray();
    try
    {
      if (DataConnection.connect())
      {
        String statement = "select id from holiday";
        PreparedStatement stmt = DataConnection.getConnection()
            .prepareStatement(statement);
        ResultSet results = stmt.executeQuery();
        while (results.next())
        {
          JA.put(new Holiday(results.getLong("id")).toJSONObject());
        }
      }
    } catch (SQLException e)
    {
      e.printStackTrace();
    }
    return JA.toString();
  }

}
